/**
 * Created by djh on 2016/12/1.
 * 分页事件 采用pajax方法
 *
 */
define(function(require,exports){
    var common = require("common");
    /**
     * pajax功能
     * @param url
     * @param fn
     */
    exports.pAjax=function (url,fn) {
        common.ajaxHandle({
            method:"get",
            url:url,
            data:{ajax:1},
            dataType:"html",
            callback:function (html) {
                if(typeof fn == "function"){
                    fn(html);
                }
            }
        });
    };

    /**
     * pajax页面加载
     * @param $list 列表对象
     * @param pageNavTag 链接选择器
     * @param title 页面标题
     * @param fn 回调入口
     */
    exports.pageAjax=function ($list,pageNavTag,fn) {
        var $panel = $list.data("nav_type")=="tab" ? $(document) : $list;
        $panel.on("click",pageNavTag,function (event) {
            var $this=$(this);
            var url=$this.attr("href");
            //单元块分页
            if($list.data("type")=="cell"){
                exports.pAjax(url,function (html) {
                    $list.html(html);
                    if(typeof fn == "function"){
                        fn($this);
                    }
                });
                return false;
            }
            var title=document.title;
            //前缀标题
            if($this.data('title')!=undefined && $this.data('title')!=""){
                var _newTypeStr=$this.data('title');
                var _oldTypeStr=title.split("-")[0].toString();
                title = title.replace(_oldTypeStr,_newTypeStr);
                document.title = title;
            }
            //全称标题
            if($this.data('alltitle')!=undefined && $this.data('alltitle')!=""){
                title=$this.data("alltitle");
                document.title=title;
            }

            if(history.pushState){
                exports.pAjax(url,function (html) {
                    //history处理
                    if(event && /\d/.test(event.button)) {
                        history.pushState({type:"ajax",title:title,url:url}, title, url);
                    }
                    $list.html(html);

                    if(typeof fn == "function"){
                        fn($this,title);
                    }
                });
                return false;
            }
        });
    };
    /**
     * 分页跳转功能
     * @param $obj 寻找分页跳转的父对象
     * @param isPajax 支持 pajax 如果是pajax方式，$ojb用列表容器对象
     */
    exports.pagesGoto=function($obj,isPajax){
        var $pagebar=$obj || $("#pageBar");
        var $pageNo=$pagebar.find('[name="pageNo"]');
        $pagebar.on('click','.pagesGoto button',function (event) {
            var $p=$(this).closest('.pagesGoto');
            $pageNo=$p.find('[name="pageNo"]');
            var pageNo=$pageNo.val();
            var type= $(this).data("type");
            var url=$(this).data("url");
            var title=document.title;

            if(url.indexOf(".html") !=-1){
                if(type==2){
                    url=url.replace(".html","-"+pageNo+".html");
                }else{
                    url=url.replace(".html","_00"+pageNo+".html");
                }
            }else{
                var joinStr= url.indexOf("?") !=-1 ? ":" : "?"
                url=url + joinStr + "pageNo=" + pageNo ;
            }

            if($pagebar.data("type")=="cell"){
                exports.pAjax(url,function (html) {
                    $pagebar.html(html);
                });
                return false;
            }

            if(isPajax && history.pushState){
                exports.pAjax(url,function (html) {
                    history.pushState({type:"ajax",title:title,url:url}, title, url);
                    $pagebar.html(html);
                });
            }else{
                window.location.href=url;
            }
        });
        $pagebar.on("keyup",'[name="pageNo"]',function (e) {
            if(!e) e = window.event;
            if((e.keyCode || e.which) == 13){
                $pagebar.find('.pagesGoto button').click();
            }
        });
    };

    //分页插件初始化
    var $wrap = $(".list_wrap");
    if($wrap.length>0) {
        $wrap.each(function(){
            exports.pageAjax($(this),".pageBar ul a");
        });
    }
});
